<template>
  <div class="welcome-container">
    <LoadingSpinner :show="loading" />
    <h1 class="title">欢迎使用Cursor无限续杯！</h1>
    <div class="path-section">
      <div class="path-display">
        <span class="path-label">Cursor 程序：</span>
        <span class="path-value">{{ cursorPath || '未选择' }}</span>
      </div>
      <button class="btn secondary" @click="selectPath" :disabled="loading">
        选择程序
      </button>
    </div>
    <div class="button-group">
      <div class="button-row">
        <Tooltip text="清除账号登录状态">
          <button class="btn secondary" @click="selectCursor" :disabled="loading">
            1.退出Cursor程序
          </button>
        </Tooltip>
        <Tooltip text="需要自己打开Cursor官网注销">
          <button class="btn secondary" @click="deleteCursor" :disabled="loading">
            2.删除Cursor绑定账号
          </button>
        </Tooltip>
      </div>
      <div class="button-row">
        <Tooltip text="重置Cursor的机器码,实现无限续杯">
          <button class="btn primary" @click="startUsing" :disabled="loading">
            3.我要续杯使用
          </button>
        </Tooltip>
        <Tooltip text="修改系统hosts文件禁止Cursor更新">
          <button class="btn secondary" @click="stopUpdate" :disabled="loading">
            禁止Cursor更新(可以不用点)
          </button>
        </Tooltip>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import LoadingSpinner from './LoadingSpinner.vue'
import Tooltip from './Tooltip.vue'

const cursorPath = ref('')
const loading = ref(false)

// 选择路径
const selectPath = async () => {
  try {
    loading.value = true
    const result = await window.electron.selectCursorPath()
    console.log('选择路径结果:', result)
    if (result && result.success) {
      cursorPath.value = result.data
      alert('路径选择成功')
    }
  } catch (error) {
    console.error('选择路径错误:', error)
    alert('选择路径失败: ' + (error.message || '未知错误'))
  } finally {
    loading.value = false
  }
}

// 获取保存的路径
const getSavedPath = async () => {
  try {
    loading.value = true
    const result = await window.electron.getCursorPath()
    console.log('获取保存路径结果:', result)
    if (result && result.success) {
      cursorPath.value = result.data
    }
  } catch (error) {
    console.error('获取保存路径错误:', error)
  } finally {
    loading.value = false
  }
}

// 组件加载时获取保存的路径
onMounted(() => {
  getSavedPath()
})

const selectCursor = async () => {
  if (!cursorPath.value) {
    // 如果没有选择路径，先选择路径
    const result = await window.electron.selectCursorPath()
    console.log('选择路径结果:', result)
    if (result && result.success) {
      cursorPath.value = result.data
    } else {
      // 用户取消选择
      return
    }
  }

  // 显示确认弹窗
  const confirmed = window.confirm('该操作将退出 Cursor 并清除所有用户数据，是否继续？')
  if (!confirmed) return

  try {
    loading.value = true
    // 处理 Cursor 程序
    const result = await window.electron.handleCursor('stop')
    if (result.success) {
      alert(result.message)
    } else {
      alert('操作失败: ' + result.message)
    }
  } catch (error) {
    alert('操作失败: ' + error.message)
  } finally {
    loading.value = false
  }
}

const deleteCursor = async () => {
  try {
    loading.value = true
    const result = await window.electron.deleteCursorAccount()
    alert(result.message)
  } catch (error) {
    alert('操作失败: ' + error.message)
  } finally {
    loading.value = false
  }
}

const startUsing = async () => {
  // 显示确认弹窗
  const confirmed = window.confirm('该操作只会重置 Cursor 的机器码，是否继续？')
  if (!confirmed) return

  try {
    loading.value = true
    const result = await window.electron.resetCursorMachineCode()
    alert(result.message)
    if (result.success) {
      // 重置成功后自动启动 Cursor
      await window.electron.handleCursor('start')
    }
  } catch (error) {
    alert('操作失败: ' + error.message)
  } finally {
    loading.value = false
  }
}

const stopUpdate = async () => {
  // 显示确认弹窗
  const confirmed = window.confirm('该操作将修改系统 hosts 文件以禁止 Cursor 更新，需要管理员权限，是否继续？')
  if (!confirmed) return

  try {
    loading.value = true
    const result = await window.electron.stopCursorUpdate()
    alert(result.message)
  } catch (error) {
    alert('操作失败: ' + error.message)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.welcome-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  color: white;
  padding: 20px;
}

.title {
  font-size: 24px;
  margin-bottom: 40px;
  text-align: center;
}

.path-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin-bottom: 30px;
  width: 100%;
  max-width: 600px;
}

.path-display {
  background-color: #2a2a2a;
  padding: 10px 15px;
  border-radius: 6px;
  width: 100%;
  word-break: break-all;
}

.path-label {
  color: #888;
  margin-right: 8px;
}

.path-value {
  color: #fff;
}

.button-group {
  display: flex;
  flex-direction: column;
  gap: 20px;
  width: 100%;
  max-width: 600px;
}

.button-row {
  display: flex;
  justify-content: center;
  gap: 20px;
}

.btn {
  padding: 12px 24px;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
  min-width: 200px;
}

.btn:hover:not(:disabled) {
  transform: translateY(-2px);
}

.btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.primary {
  background-color: #0099ff;
  color: white;
}

.primary:hover:not(:disabled) {
  background-color: #0088ee;
}

.secondary {
  background-color: #333333;
  color: white;
}

.secondary:hover:not(:disabled) {
  background-color: #444444;
}
</style> 